<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/22
 * Time: 14:10
 */

namespace app\admin\model;


use think\Model;

class Member extends Model
{

    /*
     * 编辑管理员
     * @param [type] $data [修改数据]
     * @return boolean
     */
    public function editMember($data)
    {
        if (empty($data) || !isset($data['userid']) || !is_array($data)) {
            $this->error = '没有修改的数据！';
            return false;
        }
        $info = $this->where(array('id' => $data['userid']))->find();
        if (empty($info)) {
            $this->error = '该用户不存在！';
            return false;
        }
        //密码为空，表示不修改密码
        if (isset($data['password']) && empty($data['password'])) {
            unset($data['password']);
        } else {
            $passwordinfo = md5($data['password']); //对密码进行处理
//            $data['encrypt'] = $passwordinfo['encrypt'];
            $data['password'] = $passwordinfo;
        }
        $userId = $data['userid'];
        unset($data['userid']);
        unset($data['password_confirm']);

        $status = $this->where(['id' => $userId])->update($data);
        return $status !== false ? true : false;
    }

    /**
     * 创建管理员
     * @param type $data
     * @return boolean
     */
    public function createMember($data)
    {
        if (empty($data)) {
            $this->error = '没有数据！';
            return false;
        }
        $passwordinfo = md5($data['password']); //对密码进行处理
        $data['password'] = $passwordinfo;
        $data['create_time'] = time();
        $id = $this->allowField(true)->save($data);
        if ($id) {
            return $id;
        }
        $this->error = '入库失败！';
        return false;
    }

}